﻿// Copyright 2009, Google Inc. All Rights Reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * This code sample retrieves the data for specified dimensions/metrics:
 *  1. data with implicit table id for specific date range;
 *  2. data with explicit table id for specific date range w/o metrics;
 *  3. visitor list limited with max-records;
 *  4. transaction list sorted by Id (descending order);
 *  5. totals aggregated by month for specific date range sorted by one metric, limited with max-records;
 *
 * To generate an XML Web Service Proxy Class you need to:
 * 1. Run Web Services Description Language Tool Wsdl.exe from "$Microsoft Visual Studio Install Directory$\SDK\v2.0\Bin".
 *    wsdl http://server[:port]/services/v1/reportservice?wsdl /language:CS /protocol:SOAP /namespace:SampleCode.com.google.DataAPI.ReportService /out:ReportService.cs
 * 2. Copy obtained ReportService.cs into the project folder and add it to the project.
 */

using SampleCode.com.google.DataAPI.ReportService;

using System;

namespace ReportServiceGetDataSOAP
{
	public class GetData
	{
		// Set up date format.
		private const string DATE_FORMAT = "yyyy-MM-dd";
		// Provide login information.
		private const string login = "YOUR_LOGIN";
		private const string password = "YOUR_PASSWORD";
		private const int profileId = 16;
		private const string startDate = "2000-01-01";
		private const string endDate = "2009-01-01";
		private const string dimensions = "u:visitor_id";
		private const string metrics = "u:pages,u:visits";
		private const int maxResults = 5;

		static void Main()
		{
			// Retrieve data with implicit table id for specific date range.
			GetDataListWithImplicitTable();
			// Retrieve data with explicit table id for specific date range w/o metrics.
			GetDataListWithExplicitTable();
			// Retrieve visitor list limited with max-records.
			GetDataListLimited();
			// Retrieve transaction list sorted by Id (descending order).
			GetDataListSorted();
			// Retrieve totals aggregated by month for specific date range sorted by one metric, limited with max-records.
			GetDataListAggregated();
		}

		/**
		 * This method retrieves data with implicit table id for specific date range.
		 */
		private static void GetDataListWithImplicitTable()
		{
			try
			{
				// Set up service connection.
				reportservice reportService = new reportservice();
				// Get data.
				Data[] data = reportService.getData(
					login,
					password,
					profileId.ToString(),
					0,
					false,
					0,
					false,
					DateTime.ParseExact(startDate, DATE_FORMAT, null),
					DateTime.ParseExact(endDate, DATE_FORMAT, null),
					dimensions,
					metrics,
					null,
					null,
					0,
					false);
				// Display data info.
				DisplayDataListWithImplicitTable(data);
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
			}
		}

		private static void DisplayDataListWithImplicitTable(Data[] data)
		{
			Console.WriteLine("Data retrieved with implicit table id for specific date range:");
			foreach (Data item in data)
			{
				Console.WriteLine("Record id is \"" + item.recordId + "\", ");
				// Display data dimensions.
				Console.WriteLine("dimensions are:");
				foreach (DataDimension dataDimension in item.dimensions)
					Console.WriteLine(" \tname: \"" + dataDimension.name + "\", string: " + dataDimension.Value);
				// Display data metrics (Hits, Bytes).
				Console.WriteLine("metrics are:");
				Console.WriteLine("\tpages: " + item.metrics.pages + ", visits: " + item.metrics.visits);
			}
		}

		/**
		 * This method retrieves data with explicit table id for specific date range w/o metrics.
		 */
		private static void GetDataListWithExplicitTable()
		{
			try
			{
				// Set up service connection.
				reportservice reportService = new reportservice();
				// Get data.
				Data[] data = reportService.getData(
					login,
					password,
					profileId.ToString(),
					0,
					false,
					0,
					false,
					DateTime.ParseExact(startDate, DATE_FORMAT, null),
					DateTime.ParseExact(endDate, DATE_FORMAT, null),
					dimensions,
					null,
					null,
					null,
					258,
					true);
				// Display data info.
				DisplayDataListWithExplicitTable(data);
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
			}
		}

		private static void DisplayDataListWithExplicitTable(Data[] data)
		{
			Console.WriteLine("Data retrieved with explicit table id for specific date range w/o metrics:");
			foreach (Data item in data)
			{
				Console.WriteLine("Record id is \"" + item.recordId + "\", ");
				// Display data dimensions.
				Console.WriteLine("dimensions are:");
				foreach (DataDimension dataDimension in item.dimensions)
					Console.WriteLine(" \tname: \"" + dataDimension.name + "\", string: " + dataDimension.Value);
			}
		}

		/**
		 * This method retrieves visitor list limited with max-records.
		 */
		private static void GetDataListLimited()
		{
			try
			{
				// Set up service connection.
				reportservice reportService = new reportservice();
				// Get data.
				Data[] data = reportService.getData(
					login,
					password,
					profileId.ToString(),
					0,
					false,
					maxResults,
					true,
					DateTime.ParseExact(startDate, DATE_FORMAT, null),
					DateTime.ParseExact(endDate, DATE_FORMAT, null),
					dimensions,
					null,
					null,
					null,
					0,
					false);
				// Display data info.
				DisplayDataListSorted(data);
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
			}
		}

		private static void DisplayDataListLimited(Data[] data)
		{
			Console.WriteLine("Visitor list limited with max-records (" + maxResults + "):");
			foreach (Data item in data)
			{
				Console.WriteLine("Record id is \"" + item.recordId + "\", ");
				// Display data dimensions.
				Console.WriteLine("dimensions are:");
				foreach (DataDimension dataDimension in item.dimensions)
					Console.WriteLine(" \tname: \"" + dataDimension.name + "\", string: " + dataDimension.Value);
			}
		}

		/**
		 * This method retrieve transaction list sorted by Id (descending order).
		 */
		private static void GetDataListSorted()
		{
			try
			{
				// Set up service connection.
				reportservice reportService = new reportservice();
				// Get data.
				Data[] data = reportService.getData(
					login,
					password,
					profileId.ToString(),
					0,
					false,
					0,
					false,
					DateTime.ParseExact(startDate, DATE_FORMAT, null),
					DateTime.ParseExact(endDate, DATE_FORMAT, null),
					"u:transaction_id",
					"u:transactions,u:revenue",
					"-u:transactions",
					null,
					258,
					true);
				// Display data info.
				DisplayDataListSorted(data);
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
			}
		}

		private static void DisplayDataListSorted(Data[] data)
		{
			Console.WriteLine("Transaction list sorted by Id desc:");
			foreach (Data item in data)
			{
				Console.WriteLine("Record id is \"" + item.recordId + "\", ");
				// Display data dimensions.
				Console.WriteLine("dimensions are:");
				foreach (DataDimension dataDimension in item.dimensions)
					Console.WriteLine(" \tname: \"" + dataDimension.name + "\", string: " + dataDimension.Value);
				// Display data metrics (Ttransactions, Revenue).
				Console.WriteLine("metrics are:");
				Console.WriteLine("\ttransactions: " + item.metrics.transactions + ", revenue: " + item.metrics.revenue);
			}
		}

		/**
		 * This method retrieves totals aggregated by month for specific date range sorted by one metric, 
		 * limited with max-records.
		 */
		private static void GetDataListAggregated()
		{
			try
			{
				// Set up service connection.
				reportservice reportService = new reportservice();
				// Get data.
				Data[] data = reportService.getData(
					login,
					password,
					profileId.ToString(),
					0,
					false,
					maxResults,
					true,
					DateTime.ParseExact(startDate, DATE_FORMAT, null),
					DateTime.ParseExact(endDate, DATE_FORMAT, null),
					"u:month",
					"u:hits,u:bytes",
					"u:hits",
					null,
					258,
					true);
				// Display data info.
				DisplayDataListAggregated(data);
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
			}
		}

		private static void DisplayDataListAggregated(Data[] data)
		{
			Console.WriteLine("Totals aggregated by month for specific date range sorted by one metric, limited with max-records (" + maxResults + "):");
			foreach (Data item in data)
			{
				Console.WriteLine("Record id is \"" + item.recordId + "\", ");
				// Display data dimensions.
				Console.WriteLine("dimensions are:");
				foreach (DataDimension dataDimension in item.dimensions)
					Console.WriteLine(" \tname: \"" + dataDimension.name + "\", string: " + dataDimension.Value);
				// Display data metrics (Hits, Bytes).
				Console.WriteLine("metrics are:");
				Console.WriteLine("\thits: " + item.metrics.hits + ", bytes: " + item.metrics.bytes);
			}
		}
	}
}
